package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactDate;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<ContactDate> cachedContacts;
	
	public SortedListOf<ContactDate> getContacts() {
		if (cachedContacts == null) {
			rebuildContactsCache();
		}
		return cachedContacts;
	}
	
	private void rebuildContactsCache() {
		SortedListOf<ContactDate> cachedContacts = new SortedListOf<ContactDate>();
		
		manager.navigateTo().mainPage();
		List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			String temp = title.substring("Select (".length(), title.length() - ")".length());
			String firstname = temp.trim();
			String lastname = null;
			cachedContacts.add(new ContactDate()
			.withFirstname(firstname)
			.withLastname(lastname));
		}
	}

	public ContactHelper createContact(ContactDate contact, boolean formType) {
		manager.navigateTo().mainPage();
		initContactCreation();
	    fillContactForm(contact, CREATION);
	    submitContactCreation();
	    rebuildContactsCache();
		manager.navigateTo().returnToMainPage();
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactDate contact, boolean formType) {
		manager.navigateTo().mainPage();
		goToContactEdit(index);
        fillContactForm(contact, MODIFICATION);
        submitContactModification();
        rebuildContactsCache();
        manager.navigateTo().returnToMainPage();
		return this;
	}
	
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
		goToContactEdit(index);
        deleteContact();
        rebuildContactsCache();
        manager.navigateTo().returnToMainPage();
		return this;
		
	}
	
	//--------------------------------------------------------------------------------------------------------
	
	public ContactHelper fillContactForm(ContactDate contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.adress);
		type(By.name("home"), contact.home);
		type(By.name("mobile"), contact.mobile);
	    //selectByText(By.name("bday"), contact.bday);
	    //selectByText(By.name("bmonth"), contact.bmonth);
	   	type(By.name("byear"), contact.byear);
	   	if (formType == CREATION){
	   	  //selectByText(By.name("new_group"), frends.new_group);
	   	} else {
	   		if (driver.findElements(By.name("new_group")).size() != 0) {
	   			throw new Error("Group selector exists in contact modification form");
	   		}
	   	}	
	    type(By.name("address2"), contact.address2);
		type(By.name("phone2"), contact.phone2);
		return this;
	}

	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper goToContactEdit(int index) {
		click(By.xpath("(//img[@alt='Edit'])[" + (index+1) + "]"));
		return this;
	}

	public void deleteContact() {
		click(By.xpath("(//input[@name='update'])[2]"));
		cachedContacts = null;
	}

	public void submitContactModification() {
		click(By.xpath("(//input[@name='update'])[1]"));
		cachedContacts = null;
	}

	public ContactDate getStringContact(ContactDate contact) {
		String tmp = contact.getFirstname() + " " + contact.getLastname();
		String firstname = tmp.trim();
		String lastname = null;
		return contact
				.withFirstname(firstname)
				.withLastname(lastname);
	}
					
}

